package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Acl_permission;
import com.ws.lv.service.Acl_permissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/acl_permission")
public class Acl_permissionController {
    @Autowired
    Acl_permissionService acl_permissionService;

    // 查询权限
    @RequestMapping("/selectAcl_permissions")
    public String selectAcl_permissions(String name, String path, Model model) {
        System.out.println("去查询权限页面");
        List<Acl_permission> acl_permissions = acl_permissionService.selectAcl_permissions(name, path);
        model.addAttribute("acl_permissions", acl_permissions);
        model.addAttribute("name", name);
        model.addAttribute("path", path);
        return "adminpage/acl_permission/list";
    }

    //删除权限
    @RequestMapping("/deleteAcl_permission")
    public String deleteAcl_permission(Integer perm_id, Model model) {
        System.out.println("删除权限");
        Acl_permission acl_permission = acl_permissionService.selectAcl_permissionById(perm_id);
        if (acl_permission.getPid() == 0){
            List<Acl_permission> acl_permissions = acl_permissionService.selectAcl_permissionByPid(perm_id);
            for (Acl_permission children : acl_permissions) {
                acl_permissionService.deleteAcl_role_permissionByPermId(children.getPerm_id());
                acl_permissionService.deleteAcl_permission(children.getPerm_id());
            }
        }
        //删除中间表
        acl_permissionService.deleteAcl_role_permissionByPermId(perm_id);
        //删除权限
        acl_permissionService.deleteAcl_permission(perm_id);
        return "redirect:/acl_permission/selectAcl_permissions";
    }

    // 跳转添加权限页面
    @RequestMapping("/toInsertAcl_permissionParent")
    public String toInsertAcl_permissionParent(Integer perm_id,Model model) {
        System.out.println("去添加权限页面");
        if (perm_id != null){
            System.out.println("增加子权限");
            Acl_permission parent = acl_permissionService.selectAcl_permissionById(perm_id);
            model.addAttribute("parent",parent);
        }else {
            System.out.println("增加父权限");
        }
        return "adminpage/acl_permission/add";
    }
    
    // 添加权限
    @RequestMapping("/insertAcl_permissionParent")
    public String insertAcl_permissionParent(Acl_permission acl_permission,Integer pid) {
        System.out.println("添加权限");
        if (pid == null){
            System.out.println("增加父权限");
            acl_permissionService.insertAcl_permissionParent(acl_permission);
        }else{
            System.out.println("增加子权限");
            acl_permissionService.insertAcl_permissionChild(acl_permission);
        }
        return "redirect:/acl_permission/selectAcl_permissions";
    }

    // 跳转修改权限页面
    @RequestMapping("/toUpdateAcl_permission")
    public String toUpdateAcl_permission(Integer perm_id, Model model) {
        System.out.println("去修改权限页面");
        Acl_permission acl_permission = acl_permissionService.selectAcl_permissionById(perm_id);
        model.addAttribute("acl_permission", acl_permission);
        return "adminpage/acl_permission/update";
    }
    
    // 修改权限
    @RequestMapping("/updateAcl_permission")
    public String updateAcl_permission(Acl_permission acl_permission) {
        System.out.println("修改权限");
        int i = acl_permissionService.updateAcl_permission(acl_permission);
        return "redirect:/acl_permission/selectAcl_permissions";
    }








}
